﻿using CQIE.TES.IService;
using CQIE.TES.Model;
using CQIE.TES.WEB.USER.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.UI.WebControls.Expressions;

namespace CQIE.TES.WEB.ADMIN.Controllers.Class
{
    public class Class_APIController : ApiController
    {

        /// <summary>
        /// 面向接口编程，采用注入的方式减少类的实例化
        /// </summary>
        public IClass_Service Class_Service { get; set; }


        
        /// <summary>
        /// 获取班级信息，实现分页查询功能
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">每页限制显示数</param>
        /// <param name="search">查询参数</param>
        /// <returns></returns>
        //分页、查询接口
        [HttpGet]
        [Route("api/getallclass_")]
        public object GetClass_Info(int page, int limit, string search, int isGraduate)
        {
            Dictionary<int, ENUM_Graduate> myDictionary = new Dictionary<int, ENUM_Graduate>();
            myDictionary.Add(0, ENUM_Graduate.All);
            myDictionary.Add(1, ENUM_Graduate.Yes);
            myDictionary.Add(2, ENUM_Graduate.No);

            //查询为null判断
            if (search == null)
            {
                search = "";
            }
            int count;

            List<Class_> list = Class_Service.GetClass_Page(page, limit, search, out count,myDictionary[isGraduate]);

            var data = new
            {
                code = 0,
                count,
                message = "成功",
                data = list.Select(cla => 
                
                
                new
                {
                    id = cla.ID,
                    classCode = cla.ClassCode,
                    grade = cla.Grade,
                    major = cla.Major,
                    isGraduate = cla.IsGraduate ==false?"未毕业":"已毕业"
                })

            };

            return data;
        }



        /// <summary>
        /// 添加班级信息
        /// </summary>
        /// <param name="id">根据id编辑</param>
        /// <param name="data">传入的json数据</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/insertclass_")]
        public void InsertClass_([FromBody] JObject data)
        {
            
            Class_Service.InsertClass_(data);
        }
     

        /// <summary>
        /// 是否结业（逻辑删除）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //通过时间戳，到了时间自动变更数据毕业
        [HttpPost]
        [Route("api/deleteclass_")]
        public bool DeleteClass_([FromBody] JObject id)
        {
            int w = Convert.ToInt32(id["id"]);
            return Class_Service.DeleteClass_(Convert.ToInt32(id["id"]));
        }

        [HttpGet]
        [Route("api/getcourses")]
        public object GetCourseInfo(int classCode) {

            return 0;
        }

        /// <summary>
        /// 获取班级ID和classCode给学生
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/getclassidtostudent")]
        public object GetClassIDToStudent()
        {
            var query = Class_Service.GetClassIDToStudent();

            var data = new
            {
                data = query.Select(cla => new
                {
                    id = cla.ID,
                    classCode = cla.ClassCode
                })
            };
            return data;
        }
    }
}
